package com.spirit.mypopup.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.core.content.ContextCompat;

import com.spirit.mypopup.R;

import java.util.List;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2019-10-20
 * desc   :
 */
public class QAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> strings;
    private int selectPosi = -1;
    private LayoutInflater layoutInflater;

    public QAdapter(Context mContext, List<String> strings) {
        this.mContext = mContext;
        this.strings = strings;
        this.layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return strings.size();
    }

    @Override
    public String getItem(int position) {
        return strings.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_text, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        if (selectPosi == position) {
            viewHolder.mTextView.setTextColor(ContextCompat.getColor(convertView.getContext(), R.color.colorAccent));
        } else {
            viewHolder.mTextView.setTextColor(ContextCompat.getColor(convertView.getContext(), R.color.black));
        }

        viewHolder.mTextView.setText(strings.get(position));

        return convertView;
    }

    public int getSelectPosi() {
        return selectPosi;
    }

    public void setPosition(int posi) {
        this.selectPosi = posi;
        notifyDataSetChanged();
    }


}
